/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package it.univaq.iw.jweb;

import it.univaq.iw.jweb.data.CapacitaCompetenza;
import it.univaq.iw.jweb.data.Curriculum;
import it.univaq.iw.jweb.data.DataAccess;
import it.univaq.iw.jweb.data.Servizio;
import it.univaq.iw.jweb.utils.SecurityHelpers;
import it.univaq.iw.jweb.utils.TemplateManager;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author stefano
 */
public class ModificaCapacitaCompetenze extends HttpServlet {

    private TemplateManager tpl;
    private DataAccess da;

    @Override
    public void init(ServletConfig config) throws ServletException {
        super.init(config);

        da = DataAccess.getInstance();

        tpl = TemplateManager.getInstance(getServletContext());
        tpl.setOutlineTemplate("outline");
        tpl.setDefaultData("author", "Del Vecchio - Bevini - Delfino");
        tpl.setDefaultData("homepage_url", "home");
        tpl.setDefaultData("logged", true);
    }

    private void selezionaServizi (String email) throws ServletException {

        List<Servizio> servizi = da.searchServiziGruppo(da.searchNomeGruppo(email));

        /* preparo i servizi in base allo stato del candidato */
        Iterator<Servizio> it = servizi.iterator();
        while (it.hasNext()) {
            Servizio servizio = it.next();
            if (da.curriculumCompletoByMail(email)) { /* curriculum completato */

                if (servizio.getNome().equalsIgnoreCase("inserisciCurriculum")
                        || (da.hasPreferences(email) && servizio.getNome().equalsIgnoreCase("inserisciPreferenze"))) {

                    it.remove();

                } else if(servizio.getNome().equalsIgnoreCase("inserisciCurriculum")
                        || (!da.hasPreferences(email) && servizio.getNome().equalsIgnoreCase("modificaPreferenze"))) {

                    it.remove();
                }

            } else { /* curriculum incompleto */

                if (servizio.getNome().equalsIgnoreCase("inserisciPreferenze")
                        || servizio.getNome().equalsIgnoreCase("modificaPreferenze")
                        || servizio.getNome().equalsIgnoreCase("modificaCurriculum")
                        || servizio.getNome().equalsIgnoreCase("visualizzaCurriculum")
                        || servizio.getNome().equalsIgnoreCase("eliminaCurriculum")) {

                    it.remove();
                }
            }
        }
        tpl.setDefaultData("servizi", servizi);

    }

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");

        HttpSession s = SecurityHelpers.checkSession(request);

        if (s == null) {

            response.sendRedirect("home");

        } else {

            selezionaServizi(s.getAttribute("email").toString());
            PrintWriter out = response.getWriter();

            try {



                int idCurr = da.searchIdCurriculumByMail(s.getAttribute("email").toString());
                Map datamodel = tpl.getDefaultDataModel();

                if (!request.getParameterMap().containsKey("t")) {

                    Curriculum curr = da.searchCurriculumById(idCurr);

                    CapacitaCompetenza cc = curr.getCapacitaCompetenze();

                    datamodel.put("idCurriculum", idCurr);
                    datamodel.put("cc", cc);
                    datamodel.put("action", "modificaCapacitaCompetenze?t=m");
                    tpl.process("modificaCapacitaCompetenze", datamodel, out);
                    out.close();
                    return;

                } else {

                    if (request.getParameter("Conferma") != null) {


                        if ((request.getParameter("sociali") != null && !request.getParameter("sociali").isEmpty())
                                || (request.getParameter("organizzative") != null && !request.getParameter("organizzative").isEmpty())
                                || (request.getParameter("informatiche") != null && !request.getParameter("informatiche").isEmpty())
                                || (request.getParameter("tecniche") != null && !request.getParameter("tecniche").isEmpty())
                                || (request.getParameter("artistiche") != null && !request.getParameter("artistiche").isEmpty())
                                || (request.getParameter("altro") != null && !request.getParameter("altro").isEmpty())) {

                            String sociali = (request.getParameter("sociali").isEmpty()) ? new String() : request.getParameter("sociali");
                            String organizzative = (request.getParameter("organizzative").isEmpty()) ? new String() : request.getParameter("organizzative");
                            String informatiche = (request.getParameter("informatiche").isEmpty()) ? new String() : request.getParameter("informatiche");
                            String tecniche = (request.getParameter("tecniche").isEmpty()) ? new String() : request.getParameter("tecniche");
                            String artistiche = (request.getParameter("artistiche").isEmpty()) ? new String() : request.getParameter("artistiche");
                            String altro = (request.getParameter("altro").isEmpty()) ? new String() : request.getParameter("altro");

                            da.modificaCapacitaCompetenza(new CapacitaCompetenza(
                                    sociali,
                                    organizzative,
                                    informatiche,
                                    tecniche,
                                    artistiche,
                                    altro), request.getParameter("idCurriculum"));

                        } else {

                            da.cancellaCapacitaCompetenze(request.getParameter("idCurriculum"));

                            da.completaCurriculum(new Integer(da.searchIdCurriculumByMail(s.getAttribute("email").toString())).toString());
                        }
                    }

                    response.sendRedirect("modificaCurriculum");
                }
            } finally {
                out.close();
            }
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
